package com.zr.inquiry.controller;

import com.zr.inquiry.model.*;
import com.zr.inquiry.service.InquiryService;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2019/12/27.
 */
@CrossOrigin
@RestController
public class InquiryController {

    @Autowired
    private InquiryService inquirySer;

    /*供应商下拉框*/
    @GetMapping("partner/queryByLegalPerson")
    public ResultVo supplierXl(String legalPersonId){
        legalPersonId = "1";
        legalperson lePer = inquirySer.getPersonById(1);


        List<Supplier> list = inquirySer.getSupplier(lePer.getLegalPersonCode());
        return ResultVoBuilder.success(list);
    }

    /*头表分页*/
    @PostMapping("inquiryList/search")
    public ResultVo queryPage(@RequestBody inquirytotal inquirytotal, SupplierPage supplierPage, SparePage sparePage){

        ResultVo resultVo = inquirySer.getPage(inquirytotal);

        return resultVo;
    }
    /*备件表分页*/
    @PostMapping("material/search")
    public ResultVo querySparePage(@RequestBody SparePage sparePage){
        ResultVo resultVo = inquirySer.getSparPage(sparePage);
        /*System.out.println(1/0);*/
        return resultVo;
    }

    @PostMapping("inquiryList/selectMaterial")
    public ResultVo querySparePage1(@RequestBody SparePage sparePage){
        ResultVo resultVo = inquirySer.getSparPage(sparePage);
        /*System.out.println(1/0);*/
        return resultVo;
    }

    /*供应商表分页*/
    @PostMapping("material/searchPartnerPage")
    public ResultVo querySupplierPage(@RequestBody SupplierPage supplierPage){
        ResultVo resultVo = inquirySer.getSupPage(supplierPage);
        return resultVo;
    }
    /*作废接口*/
    @PostMapping("inquiryList/modifyState")
    public ResultVo deleteInquiry(@RequestBody @Valid InquiryDelete inquiryDelete, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVoBuilder.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo =  inquirySer.deleteInquiry(inquiryDelete);
            return ResultVoBuilder.success();

    }

    /*查看接口*/
    @PostMapping("inquirySheet/see")
    public ResultVo seeInquiry(@RequestBody RequestForQuotationId questForQuotationId, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVoBuilder.error(bindingResult.getFieldError().getDefaultMessage());
        }


        List<inquirytotal> list = inquirySer.queryById(questForQuotationId.getRequestForQuotationId());

        if (list.size()<=0){
            return ResultVoBuilder.error("输入的订单号不存在");
        }else if (list.size()>1){

            return ResultVoBuilder.error("输入的订单号有重复数据");

        }else{
            /*System.out.println(list.get(0));*/

        list.get(0).getId();

        List<Object> list1 =new ArrayList();
            /*查询头表*/
            inquiryend inquiryend = inquirySer.seeInquiryTotal(list.get(0).getId());
            /*查询询价备件表*/
            List<InquirySpare> inquirySpare = inquirySer.seeSpareInquiry(list.get(0).getId());
            /*查询询价供应商表*/
            List<InquirySupplier> inquirySupplier =  inquirySer.seeSupplierInquiry(list.get(0).getId());

            inquiryend.setInquiryDetailsList(inquirySpare);
            inquiryend.setSupplierDetailsList(inquirySupplier);


            list1.add(inquiryend);

            return ResultVoBuilder.success(list1);
            /*return ResultVoBuilder.success(inquiryend);*/


        }
    }

    /*审核接口*/
    @PostMapping("inquiryAuditor/update")
    public ResultVo shenHeInquiry(@RequestBody @Valid ShenHeInquiry shenHeInquiry, BindingResult bindingResult){

        System.out.println(shenHeInquiry);
        ResultVo resultVo =  inquirySer.shenHe(shenHeInquiry);


        StatusUpdate su = new StatusUpdate();
        su.setInquiryStatus(30);
        su.setRequestForQuotationId(shenHeInquiry.getRequestForQuotationId());

        updateStatus(su);
        return resultVo;

    }
    /*修改状态的方法*/
    public ResultVo updateStatus(StatusUpdate statusUpdate){

        int i = inquirySer.updateStatus(statusUpdate);
        if (i > 0){
            return ResultVoBuilder.success("状态修改成功");
        }else{
            return ResultVoBuilder.error("修改失败");
        }
    }
    /*审批接口*/
    @PostMapping("inquiryApprover/update")
    public ResultVo passInquiry(@RequestBody PassInquiry passInquiry){
        ResultVo resultVo =  inquirySer.pass(passInquiry);

        /*System.out.println(1/0);*/

        StatusUpdate su = new StatusUpdate();
        su.setInquiryStatus(100);
        su.setRequestForQuotationId(passInquiry.getRequestForQuotationId());

        updateStatus(su);
        return resultVo;
    }

    /*修改接口*/
    @PostMapping("inquiryList/update")
    public ResultVo updateInquiry(@RequestBody inquiryend inquiry){

        System.out.println(inquiry);
        /*通过订单号获得头表信息*/
        String requestForQuotationId = inquiry.getRequestForQuotationId();
        inquirytotal inquirytotal = inquirySer.getByRid(requestForQuotationId);
        /*获取头表的主键id*/
        int inid = inquirytotal.getId();

        /*根据id,删除子表中对应的信息(数据库中)*/
        inquirySer.deleteInSpare(inid);
        inquirySer.deleteInSupplier(inid);

        /*添加信息(将前台传过来的数据,添加到数据库中)*/
        List<InquirySpare> spareList = inquiry.getInquiryDetailsList();
        List<InquirySupplier> supplierList = inquiry.getSupplierDetailsList();

        for (InquirySpare spare:spareList) {
            spare.setInquiryId(inid);
        }
        for (InquirySupplier supplier:supplierList) {
            supplier.setInquiryId(inid);
        }

        int isp = inquirySer.insertInSpare(spareList);
        int isu = inquirySer.insertInSup(supplierList);

        /*修改询价状态*/
        StatusUpdate su = new StatusUpdate();
        su.setInquiryStatus(20);
        su.setRequestForQuotationId(requestForQuotationId);
        updateStatus(su);

        /*修改头表*/
        ResultVo resultVo = inquirySer.updateInquiryTotal(inquiry);
        return ResultVoBuilder.success("iEnd");
    }

    /*新增接口*/
    @PostMapping("inquiryList/add")
    public ResultVo updateInquiry(@RequestBody @Valid inquiryend inquiry,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVoBuilder.error(bindingResult.getFieldError().getDefaultMessage());
        }

        String s = UUID.randomUUID().toString();
        inquiry.setRequestForQuotationId(s);
        return inquirySer.addInquiry(inquiry);
    }

    /*导入接口*/
    @PostMapping("quotation/InquiryIntroduction")
    public ResultVo daoRu(MultipartFile file) throws Exception{
        return inquirySer.importPlanClass(file);

    }
}
